package org.mindrive.system.web.system;

import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.mindrive.system.annotation.LogOperation;
import org.mindrive.system.core.Result;
import org.mindrive.system.parameter.BaseSearchParameter;
import org.mindrive.system.parameter.system.DictionaryParameter;
import org.mindrive.system.parameter.system.SearchDictionaryParameter;
import org.mindrive.system.service.BaseService;
import org.mindrive.system.service.system.DictionaryService;
import org.mindrive.system.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author houwei
 */
@Api(tags = {"字典表管理"})
@RestController
@RequestMapping("/dictionary")
@Slf4j
public class DictionaryController extends BaseController {

    @Autowired
    private DictionaryService dictionaryService;

    @LogOperation(type = BaseService.AUDIT_EVENT_TYPE_CREATE, event = "新建字典值")
    @ApiOperation(value = "新建字典值", notes = "新建字典值")
    @ApiImplicitParam(name = "authorization", value = "令牌", required = true, paramType = "header")
    @PostMapping(path = "/create")
    public Result createDictionary(@Validated @RequestBody DictionaryParameter parameter, BindingResult bindingResult) {
        log.info("新建字典值开始。[{}]", JSON.toJSONString(parameter));
        Result result = new Result();

        try {
            validParams(bindingResult);
            dictionaryService.createDictionary(parameter);
            result = Result.ok();
        } catch (Exception ex) {

            log.error("新建字典值失败。", ex);
            result = Result.error(ex.getMessage());
        }
        log.info("新建字典值结束。");
        return result;
    }

    @LogOperation(type = BaseService.AUDIT_EVENT_TYPE_DELETE, event = "删除字典值")
    @ApiOperation(value = "删除字典值", notes = "删除字典值")
    @ApiImplicitParam(name = "authorization", value = "令牌", required = true, paramType = "header")
    @PostMapping(path = "/delete")
    public Result deleteDictionary(@RequestBody List<String> ids) {
        log.info("删除字典值开始。[{}]", JSON.toJSONString(ids));
        Result result = new Result();

        try {
            dictionaryService.deleteDictionary(ids);
            result = Result.ok();
        } catch (Exception ex) {

            log.error("删除字典值失败。", ex);
            result = Result.error(ex.getMessage());
        }
        log.info("删除字典值结束。");
        return result;
    }

    @LogOperation(type = BaseService.AUDIT_EVENT_TYPE_MODIFY, event = "修改字典值")
    @ApiOperation(value = "修改字典值", notes = "修改字典值")
    @ApiImplicitParam(name = "authorization", value = "令牌", required = true, paramType = "header")
    @PostMapping(path = "/modify/{id}")
    public Result modifyDictionary(@PathVariable(value = "id") String id,
            @Validated @RequestBody DictionaryParameter parameter, BindingResult bindingResult) {
        log.info("修改字典值开始。[{}],[{}]", id, JSON.toJSONString(parameter));
        Result result = new Result();

        try {
            validParams(bindingResult);
            dictionaryService.moidfyDictionary(id, parameter);
            result = Result.ok();
        } catch (Exception ex) {

            log.error("修改字典值失败。", ex);
            result = Result.error(ex.getMessage());
        }
        log.info("修改字典值结束。");
        return result;
    }

    @LogOperation(type = BaseService.AUDIT_EVENT_TYPE_SEARCH, event = "查询字典值")
    @ApiOperation(value = "查询字典值", notes = "查询字典值")
    @ApiImplicitParam(name = "authorization", value = "令牌", required = true, paramType = "header")
    @PostMapping(path = "/search")
    public Result searchDictionary(@RequestBody SearchDictionaryParameter parameter) {
        log.info("查询字典值开始。[{}]", JSON.toJSONString(parameter));
        Result result = new Result();

        try {
            Map<String, Object> data = dictionaryService.searchDictionary(parameter);
            result = Result.ok(data);
        } catch (Exception ex) {

            log.error("查询字典值失败。", ex);
            result = Result.error(ex.getMessage());
        }
        log.info("查询字典值结束。");
        return result;
    }

    @LogOperation(type = BaseService.AUDIT_EVENT_TYPE_SEARCH, event = "查询字典类型")
    @ApiOperation(value = "查询字典类型", notes = "查询字典类型")
    @ApiImplicitParam(name = "authorization", value = "令牌", required = true, paramType = "header")
    @PostMapping(path = "/search-types")
    public Result searchDictionaryTypes(@RequestBody BaseSearchParameter parameter) {
        log.info("查询字典类型开始。[{}]", JSON.toJSONString(parameter));
        Result result = new Result();

        try {
            Map<String, Object> data = dictionaryService.searchTypes(parameter);
            result = Result.ok(data);
        } catch (Exception ex) {

            log.error("查询字典类型失败。", ex);
            result = Result.error(ex.getMessage());
        }
        log.info("查询字典类型结束。");
        return result;
    }
}